package com.itheima.shop.factory;

import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: design
 * @description:
 * @author: zhanghz001
 * @create: 2020-12-15 14:33
 **/
@Data
@ConfigurationProperties(prefix = "factory")
@Component("payFactory")
public class PayFactory implements ApplicationContextAware {
    
    private Map<String, String> payChannelMap;
    private ApplicationContext applicationContext;
    
    public PayChannel createChannel(String type) {
        String beanName = payChannelMap.get(type);
        return applicationContext.getBean(beanName, PayChannel.class);
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
